package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.EnsipPatternDao;
import com.eamois.domain.manual.EnsipPattern;
import com.eamois.domain.manual.EnsipType;

public class EnsipPatternDaoImpl extends BaseDaoImpl<EnsipPattern> implements EnsipPatternDao{

	@SuppressWarnings("unchecked")
	public Collection<EnsipPattern> findAllEnsipPattern() {
		// TODO Auto-generated method stub
		return (Collection<EnsipPattern>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipPattern.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public EnsipPattern findEnsipPatternByName(final String name) {
		// TODO Auto-generated method stub
		return (EnsipPattern)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipPattern.class);
				criteria.add(Restrictions.eq("name", name));
				ArrayList<EnsipPattern> types = (ArrayList<EnsipPattern>)criteria.list();
				if (types.size() == 0) {
					return null;
				}else{
					return types.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipPattern> findEnsipPattern(final String name) {
		// TODO Auto-generated method stub
		return (Collection<EnsipPattern>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipPattern.class);
				criteria.add(Restrictions.like("name", "%"+name+"%"));
				return criteria.list();
			}
		});
	}

}
